package com.app.widgets.targetimage.sample;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.app.widgets.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.zndroid.widget.ImageView.TargetImageView;

public class TargetImageActivity extends AppCompatActivity {
    private final String TAG = "xxx";

    private EditText editText;
    private TargetImageView imageView;
    private TargetImageView imageView0;
    private TargetImageView imageView1;
    private TargetImageView imageView2;
    private TargetImageView imageView3;
    private TargetImageView imageView4_sample;
    private TargetImageView imageView4;

    /*网络图片有些特殊，我们可能会使用三方框架，而三方框架可能会对原始图进行压缩处理，导致原图大小会有误差，必须要传原图*/
    private TargetImageView imageViewUrl;
    //private String url = "http://172.17.81.30:9090/app/download?path=/mnt/data/remote/isentry/event_big/2022-12-07/4118786/SUITCASE_71_100000000_20221207T140038800.jpg";
    private String url = "https://img1.bdstatic.com/static/common/img/baidu_image_logo_2dd9a28.png";
    private float[] urlTarget = {330, 350, 630, 720};

    private final float[] otherTarget = new float[] {/*多个点*/
            100, 5, 400, 380
    };

    private final float target[][] = new float[][] {/*相对于原图标记点位置（left, top, right, bottom）*/
            {200, 10, 830, 780},
            {95, 70, 230, 230},
            {170, 30, 230, 100},
            {991, 536, 1207, 793},
            {313, 431, 485, 595}
    };
    private final int[] images = new int[] {R.mipmap.head2, R.mipmap.head3, R.mipmap.head4, R.drawable.test11, R.drawable.test22};

    int index = 0;
    int sampleImage = images[index];
    float[] sampleTarget = target[index];

    private void show() {
        sampleImage = images[index];
        sampleTarget = target[index];

        imageView.setImageResource(sampleImage);
        imageView0.setImageResource(sampleImage);
        imageView1.setImageResource(sampleImage);
        imageView2.setImageResource(sampleImage);
        imageView3.setImageResource(sampleImage);
        imageView4_sample.setImageResource(sampleImage);
        imageView4.setImageResource(sampleImage);

        imageViewUrl.clearLockers();
        GlideUrl glideUrl = new GlideUrl(url, new LazyHeaders.Builder()
                .addHeader("Authorization", "5eGX086vNj3KrKYO4%2Fz0rg%2BmjiiX4bjCy%2FB9Zu49dzU%3DArcvideo")
                .build());
        Glide
                .with(this)
                .asBitmap()
                .skipMemoryCache(true)
                .load(glideUrl)
                //此处为正确处理方式，加载的网络图片不是原图 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
                .into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        Log.i(TAG, "onResourceReady: " + resource.getWidth() + " " + resource.getHeight() + " " + transition.toString());
                        imageViewUrl.setImageBitmap(resource);

                        imageViewUrl.locking(urlTarget[0], urlTarget[1], urlTarget[2], urlTarget[3]);
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {

                    }
                });
                //此处为正确处理方式，加载的网络图片不是原图 ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑


        //此处可以模拟异常情况，加载的网络图片不是原图 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
        //        .into(imageViewUrl);
        //imageViewUrl.postDelayed(new Runnable() {
        //    @Override
        //    public void run() {
        //        imageViewUrl.locking(urlTarget[0], urlTarget[1], urlTarget[2], urlTarget[3]);
        //    }
        //}, 1000);
        //此处可以模拟异常情况，加载的网络图片不是原图 ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

        imageView.clearLockers();
        imageView.post(new Runnable() {
            @Override
            public void run() {
                imageView.locking(sampleTarget[0], sampleTarget[1], sampleTarget[2], sampleTarget[3]);
            }
        });

        imageView.postDelayed(new Runnable() {/*演示添加多个目标点*/
            @Override
            public void run() {
                Paint paint = new Paint();
                paint.setAntiAlias(true);
                paint.setDither(true);
                paint.setColor(Color.RED);
                paint.setStrokeWidth(6.0f);
                paint.setStyle(Paint.Style.STROKE);

                imageView.addLock(otherTarget[0], otherTarget[1], otherTarget[2], otherTarget[3], paint);
                imageView.locking();
            }
        }, 2000);

        imageView0.clearLockers();
        imageView0.post(new Runnable() {
            @Override
            public void run() {
                imageView0.locking(sampleTarget[0], sampleTarget[1], sampleTarget[2], sampleTarget[3]);
            }
        });

        imageView1.clearLockers();
        imageView1.post(new Runnable() {
            @Override
            public void run() {
                imageView1.locking(sampleTarget[0], sampleTarget[1], sampleTarget[2], sampleTarget[3]);
            }
        });

        imageView2.clearLockers();
        imageView2.post(new Runnable() {
            @Override
            public void run() {
                imageView2.locking(sampleTarget[0], sampleTarget[1], sampleTarget[2], sampleTarget[3]);
            }
        });

        imageView3.clearLockers();
        imageView3.post(new Runnable() {
            @Override
            public void run() {
                imageView3.locking(sampleTarget[0], sampleTarget[1], sampleTarget[2], sampleTarget[3]);
            }
        });

        imageView4.clearLockers();
        imageView4.post(new Runnable() {
            @Override
            public void run() {
                imageView4.locking(sampleTarget[0], sampleTarget[1], sampleTarget[2], sampleTarget[3]);
            }
        });

        imageView4_sample.clearLockers();
        imageView4_sample.post(new Runnable() {
            @Override
            public void run() {
                imageView4_sample.locking(sampleTarget[0], sampleTarget[1], sampleTarget[2], sampleTarget[3]);
            }
        });
    }

    private String input = "0";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_target_image);

        editText = findViewById(R.id.input);
        imageViewUrl = findViewById(R.id.img_url);
        imageView0 = findViewById(R.id.img0);
        imageView = findViewById(R.id.img);
        imageView1 = findViewById(R.id.img1);
        imageView2 = findViewById(R.id.img2);
        imageView3 = findViewById(R.id.img3);
        imageView4_sample = findViewById(R.id.img4_sample);
        imageView4 = findViewById(R.id.img4);

        editText.setHint("内置不同分辨率的图片，输入：0-" + (images.length - 1));
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                input = s.toString();
            }
        });

        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    if (TextUtils.isEmpty(input)) {
                        index = 0;
                    } else {
                        index = Integer.parseInt(input);
                        if (index >= images.length || index < 0) {
                            index = 0;
                        }
                    }

                    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                    inputMethodManager.hideSoftInputFromWindow(editText.getWindowToken(), 0);

                    show();
                    return true;
                }
                return false;
            }
        });

        show();
    }

    public void reset(View view) {
        imageViewUrl.clearLockers();
        imageView.clearLockers();
        imageView0.clearLockers();
        imageView1.clearLockers();
        imageView2.clearLockers();
        imageView3.clearLockers();
        imageView4_sample.clearLockers();
        imageView4.clearLockers();
    }
}